/**
 * Project Name:meatball-common
 * File Name:UUIDUtil.java
 * Package Name:com.meatball.common.utils
 * Date:2017年10月11日下午4:49:27
 * Copyright (c) 2017, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import java.util.UUID;

/**   
 * @Title: UUIDUtil.java 
 * @Package com.meatball.common.utils
 * @Description: TODO(生成UUID)
 * @author 張翔宇  
 * @date 2017年10月11日 下午4:49:27 
 * @version V1.0   
 */
public class UUIDUtil {

	/**
	 * @title: UUID
	 * @description: todo(生产标准的UUID)
	 * @params: []
	 * @return: java.lang.String    返回类型
	 * @author: 張翔宇
	 * @date: 2019/3/10 18:11
	 */
	public static String UUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * @title: compressedUUID
	 * @description: todo(生成带大小写字母的UUID)
	 * @params: []
	 * @return: java.lang.String    返回类型
	 * @author: 張翔宇
	 * @date: 2019/3/10 18:12
	 */
	public static String compressedUUID() {
		UUID uuid = UUID.randomUUID();
		return compressedUUID(uuid);
	}

	/**
	 * @title: compactUUID
	 * @description: todo(生成没有中横线的UUID)
	 * @params: []
	 * @return: java.lang.String    返回类型
	 * @author: 張翔宇
	 * @date: 2019/3/10 18:12
	 */
	public static String compactUUID() {
		UUID uuid = UUID.randomUUID();
		String result = uuid.toString();

		return StringUtils.remove(result, '-');

	}

	/**
	 * @title: compress
	 * @description: todo(格式化UUID为字母大小写)
	 * @params: [uuidString]
	 * @return: java.lang.String    返回类型
	 * @author: 張翔宇
	 * @date: 2019/3/10 18:14
	 */
	public static String compress(String uuidString) {
		UUID uuid = UUID.fromString(uuidString);
		return compressedUUID(uuid);
	}

	/**
	 * @title: uncompress
	 * @description: todo(格式化为标准UUID)
	 * @params: [compressedUUID]
	 * @return: java.lang.String    返回类型
	 * @author: 張翔宇
	 * @date: 2019/3/10 18:15
	 */
	public static String uncompress(String compressedUUID) {
		if (compressedUUID.length() != 22) {
			throw new IllegalArgumentException("Invalid uuid!");
		}
		byte[] byUUID = Base64.decodeBase64(compressedUUID + "==");
		long most = bytes2long(byUUID, 0);
		long least = bytes2long(byUUID, 8);
		UUID uuid = new UUID(most, least);
		return uuid.toString();
	}

	protected static String compressedUUID(UUID uuid) {
		byte[] byUUID = new byte[16];
		long least = uuid.getLeastSignificantBits();
		long most = uuid.getMostSignificantBits();
		long2bytes(most, byUUID, 0);
		long2bytes(least, byUUID, 8);
		String compressUUID = Base64.encodeBase64URLSafeString(byUUID);
		return compressUUID;
	}

	protected static void long2bytes(long value, byte[] bytes, int offset) {
		for (int i = 7; i > -1; i--) {
			bytes[offset++] = (byte) ((value >> 8 * i) & 0xFF);
		}
	}

	protected static long bytes2long(byte[] bytes, int offset) {
		long value = 0;
		for (int i = 7; i > -1; i--) {
			value |= (((long) bytes[offset++]) & 0xFF) << 8 * i;
		}
		return value;
	}
}
